package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.ControlView;
import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class ControlButton extends ControlElement {
    private static final String TAG = "ControlButton";


    float min_text_size;

    public ControlButton(ViewOptions options)
    {
        super(options);


        edge_pix = ControlView.mm2Pix(0.36f);
        edge_pix_half = edge_pix*0.5f-1;
        p.setStrokeWidth(edge_pix);

        min_text_size = ControlView.mm2Pix(1.5f);

        p.setTextAlign(Paint.Align.CENTER);
    }


    @Override
    public void showSettingsDialog(Context context) {


        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        Button ok;
        TextView info;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_button,null);
        builder.setView(layout);
        dialog = builder.create();
        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);

        if(options.on_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.on_value));
            else
               str=(""+options.on_value);
        }else
            str="";

            on_edit.setText(str);
            on_edit.setSelection(str.length());

        if(options.off_en)
        {
            if(linkedValues[0].type<4)
             str=(""+(int)(options.off_value));
            else
                str=(""+options.off_value);
        }else
            str ="";

        off_edit.setText(str);
        off_edit.setSelection(str.length());

        if(linkedValues[0].type<4)
            str = "此按钮链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此按钮链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_en = false;
                }else
                {
                    options.on_value = value.getFloat();
                    options.on_en = true;
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_en = false;
                }else
                {
                    options.off_value = value.getFloat();
                    options.off_en = true;
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;

                //Log.w(TAG, "onClick"+getNumber(on_edit.getText().toString()).get()+"   "+getNumber(off_edit.getText().toString()).get());
                dialog.dismiss();
            }
        });
        dialog.show();
    }




    float RADIUS_RATIO = 0.03f;

    float progress=0;
    RectF rectF = new RectF();

    float edge_pix;
    float edge_pix_half;
    float radius_pix;

    float pressed_x=0,pressed_y=0;
    float circle_x,circle_y;

    float text_size;
    float last_text_size;


    RectF outer_rect = new RectF();

    float text_y1,text_y2,text_y0;
    @Override
    public void onSizeChanged() {


        radius_pix = RADIUS_RATIO*(w_pix+h_pix);

        rectF.right = w_pix*0.5f-edge_pix;
        rectF.left = -rectF.right;

        rectF.bottom = h_pix*0.5f-edge_pix;
        rectF.top = -rectF.bottom;

        outer_rect = new RectF(rectF);

        outer_rect.right += edge_pix_half;
        outer_rect.left -= edge_pix_half;
        outer_rect.bottom += edge_pix_half;
        outer_rect.top -= edge_pix_half;


        text_size =h_pix*0.3f;

        if(text_size>40)
            text_size = 40;
        if(Math.abs(text_size-last_text_size)>5) {
            p.setTextSize(text_size);
            last_text_size = text_size;
        }

        text_y0 = text_size*0.5f;
        text_y1 = -h_pix*0.2f+text_size*0.37f;
        text_y2 = h_pix*0.2f+text_size*0.37f;

    }

    @Override
    public void draw(Canvas canvas) {

        if(linkedValues[0]==null)
        {
        if(isPressed)
        {
            circle_x = (pressed_x-0.5f)*w_pix;
            circle_y = (pressed_y-0.5f)*h_pix;
            progress+=(1-progress)*anim_k;
        }else
        {
            progress+=(-progress)*anim_k;
        }
        }else
        {

                if (linkedValues[0].getFloat()>options.threshold){
                    if(isPressed) {
                        circle_x = (pressed_x - 0.5f) * w_pix;
                        circle_y = (pressed_y - 0.5f) * h_pix;
                    }else
                    {
                        circle_x = 0;
                        circle_y = 0;
                    }
                    progress += (1 - progress) * anim_k;
                } else {
                    progress += (-progress) * anim_k;
                }

        }

        canvas.save();
        canvas.clipRect(rectF);
        p.setColor(ViewOptions.THEME_BACK_COLOR);
        p.setStyle(Paint.Style.FILL);
        canvas.drawRect(rectF,p);

        if(progress>0.05f) {
            p.setColor(ViewOptions.THEME_FRONT_COLOR);
            p.setAlpha((int) (255 * progress));
            canvas.drawCircle(circle_x, circle_y, (w_pix+h_pix) * progress, p);
        }

        p.setColor(getMiddleColor(ViewOptions.THEME_TEXT_COLOR,ViewOptions.THEME_TEXT_BACK_COLOR,progress));

        if(text_size>min_text_size) {
            if (linkedValues[0] == null)
                canvas.drawText("按钮", 0, text_y0, p);
            else{
                if (valuesName[0] != null)
                    canvas.drawText(valuesName[0], 0, text_y1, p);
                canvas.drawText("" + linkedValues[0].get(), 0, text_y2, p);
            }
        }

        canvas.restore();
        p.setStyle(Paint.Style.STROKE);
        p.setColor(ViewOptions.THEME_EDGE_COLOR);
        canvas.drawRoundRect(outer_rect,radius_pix,radius_pix,p);
        //canvas.drawCircle(0,0,w_pix*0.3f,p);
    }

    @Override
    public void clicked(Context context) {
        Log.w(TAG, "clicked");
    }
    @Override
    public void dragged() {

        Log.w(TAG, "dragged");
    }
    @Override
    public void released() {
        Log.w(TAG, "released");
        if(linkedValues[0]!=null&&options.off_en) {
            linkedValues[0].set(options.off_value);
            triggerDataEvent();
        }
    }
    @Override
    public void pressed() {

        pressed_x = rx;
        pressed_y = ry;

        if(linkedValues[0]!=null&&options.on_en) {
            linkedValues[0].set(options.on_value);
            triggerDataEvent();
        }
        Log.w(TAG, "pressed");
    }

    @Override
    public void zoomed(float sx, float sy) {

        Log.w(TAG, "zoomed"+sx+"  "+sy);
    }


}
